﻿#include "hhpd.h"
#include "ui_hhpd.h"

hhpd::hhpd(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::hhpd)
{
    ui->setupUi(this);

    QStringList  labels;
    labels<<QStringLiteral("参数") <<QStringLiteral("值");
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setHorizontalHeaderLabels(labels);
    ui->tableWidget->resizeColumnsToContents(); //根据内容调整列宽
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setHidden(true);
    ui->tableWidget->setInputMethodHints(Qt::ImhHiddenText);

    this->setWindowFlags(Qt::WindowStaysOnTopHint);
    this->setAttribute(Qt::WA_QuitOnClose, false);
    this->setWindowModality(Qt::ApplicationModal);
}

hhpd::~hhpd()
{
    delete ui;
}
void hhpd::showEvent(QShowEvent *event){
    refInfo();
}

void hhpd::refInfo(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="Select * From MixedJudgment";
    query.exec(sql);

    ui->tableWidget->setRowCount(0);
    int rowNum=0;
    QList<QString> lableList;
    lableList<<"Parameter"<<"Num";
    while (query.next())
    {
        ui->tableWidget->insertRow(rowNum);

        for (int col = 0; col < lableList.length(); col++) {
            QTableWidgetItem *item = new QTableWidgetItem(query.value(lableList[col]).toString());
            if(col==0){
                item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
            }
            ui->tableWidget->setItem(rowNum, col, item);
        }
        rowNum++;
    }
}

void hhpd::on_pushButton_2_clicked()
{
    this->close();
}

void hhpd::on_pushButton_3_clicked()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    for (int i=0;i<ui->tableWidget->rowCount();i++) {
        QString para=ui->tableWidget->item(i,0)->text();
        QString num=ui->tableWidget->item(i,1)->text();

        QString sql=QString("Update MixedJudgment set Num='%1' where Parameter='%2'").arg(num,para);
        query.exec(sql);

    }

    this->close();
}
